<?php
require('lib/verify.php');
require('lib/has_char.php');
require('lib/is_approved.php');
require('lib/is_admin.php');

$system_id = $system["system_id"];

$credit_id = $_POST["loot_credit"];

$sql = "select *, wm_bosses.*, wm_raids.*, wm_instances.* from wm_credits left join wm_bosses on wm_bosses.boss_id = credit_boss left join wm_raids on wm_credits.credit_raid = wm_raids.raid_id left join wm_instances on wm_instances.instance_id = wm_raids.raid_instance where credit_id = '$credit_id' and credit_system_id = $system_id order by credit_date asc";
$res = $db->query($sql);
$credit = $db->fetch_assoc($res);

$sql = "select * from wm_bosses where wm_bosses.boss_trash=1 and wm_bosses.boss_instance = " . $credit["instance_id"];
$res = $db->query($sql);
$trash = $db->fetch_assoc($res);
$trash_id = $trash["boss_id"];

$_category = "warmath";
$_title = "Admin / Add Loot: Step 2 of 2";
require('lib/head.php');

boxTop("Warmath: $_title");
?>
<b>Add Loot</b><br />
to 
<a href="credit.php?id=<?=$credit["credit_id"]?>"><?=$credit["boss_name"]?>, <?=date("g:i a", strtotime($credit["credit_date"]))?></a> in <a href="raid.php?id=<?=$credit["raid_id"]?>"><?=$credit["instance_name"]?>, <?=date("D d M Y", strtotime($credit["raid_date"]))?></a><br />

<br />
<form action="add-loot-3.php" method="post">
<input type="hidden" name="loot_credit" value="<?=$credit["credit_id"]?>">
<b>Effective Bids</b><br />
for 
<?php
$sql = "select *, item_cache.* from wm_items left join item_cache on wm_items.item_id = item_cache.item_id where wm_items.item_id = " . $_POST["loot_item"];

$result = $db->query($sql);
while($item = $db->fetch_assoc($result)) {
?>
<input type="hidden" name="loot_item" value="<?=$_POST["loot_item"]?>"><?show_link($item)?>
<?php
}

?>

<?php
if($system["system_tier"]==4) {
$perc[3] = 1;
$perc[2] = 0.6;
$perc[1] = 0.2;
$floor_max = 2;
}
else{
$perc[4] = 1;
$perc[3] = 0.6;
$perc[2] = 0.2;
$perc[1] = 0.05;
$floor_max = 0.5;
}

$ebids = array();
$bid_result = array();
$item_id = $_POST["loot_item"];

$sql = "select *, wm_characters.* from wm_wishes left join wm_characters on wm_wishes.wish_character = wm_characters.character_id where wish_item = $item_id and wish_system_id = $system_id order by wish_level desc";
$result = $db->query($sql);

while($ite = $db->fetch_assoc($result)) {
  $bid = $ite["character_dkp"] * $perc[$ite["wish_level"]];
  if($bid < ($floor_max)) { //*$perc[$ite["wish_level"]])) {
    $bid = $floor_max; //*$perc[$ite["wish_level"]];
  }
  $ebids[$ite["character_id"]] = $bid;
  $bid_result[$ite["character_id"]] = $ite;
}

$sql = "select *, wm_characters.* from wm_attendees left join wm_characters on wm_attendees.attendee_character = wm_characters.character_id where attendee_system_id = $system_id and attendee_credit = $credit_id and attendee_satout = 0 order by character_name asc";
$result = $db->query($sql);
while($chara = $db->fetch_assoc($result)) {
  if(!isset($bid_result[$chara["character_id"]])) {
    $chara["wish_level"] = 1;
    $bid_result[$chara["character_id"]] = $chara;
    $bid = $chara["character_dkp"] * $perc[$chara["wish_level"]];
    if($bid < ($floor_max)) { //*$perc[$chara["wish_level"]])) {
      $bid = $floor_max; //*$perc[$chara["wish_level"]];
    }
    $ebids[$chara["character_id"]] = $bid;
  }
}

arsort($ebids);

while(list($n, $ite) = each($ebids)) {
  $chara = $bid_result[$n];

  //check if $chara["character_id"] is an attendee
  $sql = "select * from wm_attendees where wm_attendees.attendee_credit = $credit_id and attendee_satout = 0 and wm_attendees.attendee_character = " . $chara["character_id"];

  $rezz = $db->query($sql);
  if($db->num_rows($rezz)==0) {
    continue;
  }

  if($system["system_tier"]==4) {
  $lvl[3] = "Max";
  $lvl[2] = "Mod";
  $lvl[1] = "Min";
  }
  else {
  $lvl[4] = "Max";
  $lvl[3] = "Mod";
  $lvl[2] = "Min";
  $lvl[1] = "Micro";
  }
?>
<b><?=number_format($ebids[$chara["character_id"]], 2)?></b> dkp <?=$lvl[$chara["wish_level"]]?> by <?cclass($chara["character_class"])?> <a href="character.php?id=<?=$chara["character_id"]?>"><?=$chara["character_name"]?></a> (<?=number_format($chara["character_dkp"], 2)?> dkp)<br />
<?php
}

?>



<br />
<b>Loot</b><br />
looted by 
<select name="loot_character">
<?php
$sql = "select *, wm_characters.* from wm_attendees left join wm_characters on wm_attendees.attendee_character = wm_characters.character_id where attendee_system_id = $system_id and attendee_credit = $credit_id and attendee_satout = 0 order by character_name asc";
$result = $db->query($sql);
while($chara = $db->fetch_assoc($result)) {
?>
<option value="<?=$chara["character_id"]?>"><?=$chara["character_name"]?>
<?php
    }
?>
</select>

</select>
for
<select name="loot_level">
<?php
if($system["system_tier"]==4) {
?>
<option value="1" selected>Min
<option value="2">Mod
<option value="3">Max
<?php
}
else {
?>
<option value="1" selected>Micro
<option value="2">Min
<option value="3">Mod
<option value="4">Max
<?php
    }
?>
</select>
<br />
<br />
<input type="submit" value="Add Loot">
</form>

<br /><br />
<?php
boxBot();
?>

<?php
require('lib/foot.php');
?>

